﻿using AutoMapper;
using Max.PH5_2006.ShoppingSite.Domain;
using Max.PH5_2006.ShoppingSite.DTO;
using Max.PH5_2006.ShoppingSite.IRespository;
using Max.PH5_2006.ShoppingSite.IService;
using Max.PH5_2006.ShoppingSite.Respository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2006.ShoppingSite.Service
{
    public class MemberService : BaseService<MemberEntity, MemberEntity, MemberEntity>, IMemberService
    {
        //public MemberService(IBaseRespository<MemberEntity> respository, IMapper map) : base(respository, map)
        //{
        //}

        IMemberRespository _respository;
        public MemberService(IMemberRespository respository, IMapper map) : base(respository, map)
        {
            _respository = respository;
        }

        public (bool, string, MemberEntity) Login(MemberEntity model)
        {
            var list = _respository.Query();
            bool statue = true;
            string message = "登录成功";
            MemberEntity m = new MemberEntity();
            var u = list.Where(x => x.Name == model.Name).FirstOrDefault();
            if (u != null)
            {
                if (u.Password.Equals(model.Password))
                {
                    //用户名和密码都正确
                    if (u.Statue.Equals("0"))
                    {
                        //成功
                        //正常状态
                        statue = true;
                        message = "登录成功";
                        m = u;
                    }
                    else
                    {
                        //已禁用
                        statue = false;
                        message = "已禁用";
                    }
                }
                else
                {
                    //密码错误
                    statue = false;
                    message = "密码错误";
                }
            }
            else
            {
                //用户不存在或用户名错误
                statue = false;
                message = "用户不存在或用户名错误";
            }

            //var result = _responsitory.Login(model);

            return (statue, message, m); ;
        }
    }

    public class MemberAddressService : BaseService<MemberAddressEntity, MemberAddressEntity, MemberAddressEntity>, IMemberAddressService
    {
        IMemberAddressRespository _respository;
        ICityRespository _cityRespository;
        public MemberAddressService(IMemberAddressRespository respository, IMapper map, ICityRespository cityRespository) : base(respository, map)
        {
            _respository = respository;
            _cityRespository = cityRespository;
        }


        //在添加地址的时候，还会修改其它地址为非默认
        public int AddAddress(MemberAddressEntity model)
        {
            /*
             * 需要根据待添加的这个地址中，是否需要设置为默认地址
             * 如果是：则需要把其它地址修改为非默认
             * 如果否：则直接添加
             * **/
            if (model.IsDefault.Equals("1"))
            {
                //是，则需要修改其它后再添加

                var m = _respository.Query().Where(x => x.IsDefault.Equals("1")).FirstOrDefault();
                if (m != null)
                {
                    m.IsDefault = "0";
                    _respository.Update(m); //这是将原默认地址，修改为非默认地址
                }
            }

            int result = _respository.Add(model);//修改完之后再添加

            return result;
        }


        //显示地址（详细地址和省市区进行了合并显示处理）
        public List<MemberAddressEntity> Query(string memberId)
        {
            var addr = _respository.Query().Where(x => memberId.Equals(x.MemberId.ToString()));  //这是这个会员的所有地址数据
            var city = _cityRespository.Query();  //这是所有的城市数据

            var q = from a in addr
                    join b in city on a.Provite equals b.Id.ToString()
                    join c in city on a.City equals c.Id.ToString()
                    join d in city on a.Area equals d.Id.ToString()
                    orderby a.IsDefault descending
                    select new MemberAddressEntity
                    {
                        Id = a.Id,
                        IsDefault = a.IsDefault,
                        Address = b.Name + " " + c.Name + " " + d.Name + " " + a.Address,
                        Name = a.Name,
                        MemberId = a.MemberId,
                        Phone = a.Phone,
                        Provite= a.Provite,
                        City= a.City,
                        Area= a.Area,
                    };

            return q.ToList();
        }
    }
}
